/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ch.repit.aglaja.client.contacts;

import ch.repit.rwt.client.AttributeDef;
import ch.repit.rwt.client.AttributeDef.Feature;
import ch.repit.rwt.client.AttributeType;
import ch.repit.rwt.client.Bento;
import ch.repit.rwt.client.BentoDef;
import ch.repit.rwt.client.ObjectRef;
import ch.repit.rwt.client.ui.Page;
import ch.repit.rwt.client.util.CountryCodes;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 *
 * @author tc149752
 */
public class AddressDef extends BentoDef {

    public static final String TYPE = "Address";

    public static final String ATTR_HEADER     = "header";
    public static final String ATTR_DISPLAYNAMES     = "displayNames";  
    public static final String ATTR_TAGS      = "tags";
    public static final String ATTR_PRIVATEPHONE = "privatePhone";
    public static final String ATTR_EMAIL = "email";
    public static final String ATTR_ADDRESSLINE = "addressLine";
    public static final String ATTR_ZIP = "zipCode";
    public static final String ATTR_LOCALITY = "locality";
    public static final String ATTR_COUNTRY = "country";
    public static final String ATTR_WEDDINGDATE = "weddingDate";

    private static Set<AttributeDef> attrDefs = new HashSet<AttributeDef>();
    static {
        attrDefs.add(new AttributeDef(ATTR_HEADER, AttributeType.STRING) );
        attrDefs.add(new AttributeDef(ATTR_DISPLAYNAMES, AttributeType.BOOLEAN));
        attrDefs.add(new AttributeDef(ATTR_EMAIL, "email", AttributeType.STRING));
        attrDefs.add(new AttributeDef(ATTR_PRIVATEPHONE, "t\u00E9l. perso.", AttributeType.STRING));
        attrDefs.add(new AttributeDef(ATTR_ADDRESSLINE, "rue et num\u00E9ro", AttributeType.STRING));
        attrDefs.add(new AttributeDef(ATTR_ZIP, "code postal", AttributeType.STRING, Feature.MANDATORY));
        attrDefs.add(new AttributeDef(ATTR_LOCALITY, "localit\u00E9", AttributeType.STRING, Feature.MANDATORY));
        attrDefs.add(new AttributeDef(ATTR_COUNTRY, "pays", AttributeType.STRING, CountryCodes.CH));        // set suisse as default?
        attrDefs.add(new AttributeDef(ATTR_WEDDINGDATE, "date mariage", AttributeType.DAY));
    }

    /**
     * To be used server-side, where categories are just an attribute
     */
    public AddressDef() {
        super(attrDefs);
    }


    @Override
    public String getType() {
        return TYPE;
    }

    @Override
    public String getTypeLabel() {
        return "Adresse";
    }

    @Override
    public String getDistinguishedAttribute() {
        return ATTR_HEADER;
    }

    @Override
    public String getJdoClassName() {
        return "ch.repit.aglaja.server.contacts.Address";
    }

    @Override
    public Page getViewPage(ObjectRef oref) {
        return new ContactDetailsPage(oref);
    }

    
    // ----- specific methods -----

    public String formatNames(Bento addressBento, List<Bento> personBentos) {
        Boolean fullNames = true;
        if (addressBento!=null && addressBento.get(AddressDef.ATTR_DISPLAYNAMES)!=null) {
            fullNames = addressBento.get(AddressDef.ATTR_DISPLAYNAMES).getBoolean();
            if (fullNames==null)   // should not happen
                return "";
        }
        // if false, only firstnames

        StringBuffer res = new StringBuffer();
        String comma = "";
        boolean firstline = true;
        String oldLastname = null;
        for (Iterator<Bento> i = personBentos.iterator(); i.hasNext(); ) {
            Bento p = i.next();
            String firstname = p.getDisplayValue(PersonDef.ATTR_FIRSTNAME);
            String lastname = p.getDisplayValue(PersonDef.ATTR_LASTNAME);
            if (fullNames && oldLastname != null && !oldLastname.equals(lastname))
                res.append(" " + oldLastname);
            comma = firstline?"":i.hasNext()?", ":" et ";
            res.append(comma + firstname);
            oldLastname = lastname;
            firstline = false;
        }
        if (fullNames) res.append(" " + oldLastname);
        return res.toString();
    }


}
